package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaAccountViewDao;
import com.qymerge.manage.base.ro.MetaAccountViewRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *<p>Title: CardDaoImpl</p>
 *<p>Description: 优惠券管理</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:54
 */
@Repository
public class MetaAccountViewDaoImpl extends BaseDao implements IMetaAccountViewDao {


    private String dwd_MCRS_MDM_meta_AccountView = "SELECT Code,Name,ParentCode,Type,IsLeaf,Category,Belong, CalcFlag, Etl_Last_Date FROM dwd_MCRS_MDM_meta_AccountView WHERE 1=1 ";

    @Override
    public List list(MetaAccountViewRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_AccountView;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        // 分页数据获取
        sql += " order by Code desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaAccountViewRO>(MetaAccountViewRO.class));
    }

    @Override
    public int total(MetaAccountViewRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_AccountView where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (StringUtils.isNotBlank(ro.getCode())) {
            sql += " and Code like :Code";
            paramMap.put("Code", "%" + ro.getCode() + "%");
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql += " and Name like :Name";
            paramMap.put("Name", "%" + ro.getName() + "%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaAccountViewRO findOne(String code) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_AccountView);
        sql.append("and Code=?");
        RowMapper<MetaAccountViewRO> rowMapper = new BeanPropertyRowMapper<MetaAccountViewRO>(MetaAccountViewRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, code);
        } catch (DataAccessException e) {
            return null;
        }
    }


}
